package com.mine.yyb.modules.sys.dao.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.jdbc.MineBaseDao;
import com.mine.yyb.modules.sys.dao.IReportDao;
import com.mine.yyb.modules.sys.vo.ReportVO;

/**
 * <pre>
 * <一句话功能简述>
 * 
 * 商家反馈数据报表dao接口实现
 * 
 * <功能详细描述>
 * </pre>
 * 
 * @author xuganhua
 * @version [版本号, 2016年1月2日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository
public class ReportDaoImpl extends MineBaseDao implements IReportDao
{
    private static Logger logger = LoggerFactory.getLogger(MineBaseDao.class);
    
    @Override
    public int save(ReportVO reportVO)
        throws MineException
    {
        try
        {
            logger.info(reportVO.toString());
            
            StringBuffer sql = new StringBuffer();
            sql.append(" insert into " + ReportVO.TABLE_NAME);
            sql.append(" (activity,orderDate,orderID,orderPrice,rebate,label,labelTo,orderIP,createDate)");
            sql.append(" values(:activity,:orderDate,:orderID,:orderPrice,:rebate,:label,:labelTo,:orderIP,now())");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), reportVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public int update(ReportVO reportVO)
        throws MineException
    {
        try
        {
            logger.info(reportVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append(" update " + ReportVO.TABLE_NAME);
            sql.append(" set updateDate = now()");
            if (StringUtils.isNotBlank(reportVO.getActivity()))
            {
                sql.append(" ,activity = :activity");
            }
            if (reportVO.getOrderDate() != null)
            {
                sql.append(" ,orderDate = :orderDate");
            }
            if (reportVO.getOrderPrice() != 0)
            {
                sql.append(" ,orderPrice=:orderPrice");
            }
            if (reportVO.getRebate() != 0)
            {
                sql.append(" ,rebate=:rebate");
            }
            if (StringUtils.isNotBlank(reportVO.getLabel()))
            {
                sql.append(" ,label=:label");
            }
            if (StringUtils.isNotBlank(reportVO.getLabelTo()))
            {
                sql.append(" ,labelTo=:labelTo");
            }
            if (StringUtils.isNotBlank(reportVO.getOrderIP()))
            {
                sql.append(" ,orderIP=:orderIP");
            }
            sql.append(" where orderID = :orderID");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), reportVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public ReportVO queryReport(ReportVO reportVO)
        throws MineException
    {
        try
        {
            logger.info(reportVO.toString());
            
            StringBuffer sql = new StringBuffer();
            sql.append("select id, activity, orderDate, orderID, orderPrice, rebate, label, labelTo, orderIP ");
            sql.append("from " + ReportVO.TABLE_NAME + " ");
            sql.append("where 1 = 1 ");
            if (StringUtils.isNotBlank(reportVO.getOrderID()))
            {
                sql.append("and orderID = :orderID ");
            }
            if(StringUtils.isNotBlank(reportVO.getLabel())) 
            {
                sql.append("and label = :label ");
            }
            if(StringUtils.isNotBlank(reportVO.getLabelTo())) 
            {
                sql.append("and labelTo = :labelTo ");
            }
            
            return super.queryForObject(sql.toString(), reportVO, ReportVO.class);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
}
